package org.dromara.datacheck.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.datacheck.domain.JdAndJstMap;
import org.dromara.datacheck.domain.bo.JdAndJstMapBo;
import org.dromara.datacheck.domain.vo.JdAndJstMapVo;
import org.dromara.datacheck.mapper.JdAndJstMapMapper;
import org.dromara.datacheck.service.IJdAndJstMapService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 金蝶和聚水潭映射Service业务层处理
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@RequiredArgsConstructor
@Service
public class JdAndJstMapServiceImpl implements IJdAndJstMapService {

    private final JdAndJstMapMapper baseMapper;

    /**
     * 查询金蝶和聚水潭映射
     *
     * @param id 主键
     * @return 金蝶和聚水潭映射
     */
    @Override
    public JdAndJstMapVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询金蝶和聚水潭映射列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 金蝶和聚水潭映射分页列表
     */
    @Override
    public TableDataInfo<JdAndJstMapVo> queryPageList(JdAndJstMapBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JdAndJstMap> lqw = buildQueryWrapper(bo);
        Page<JdAndJstMapVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的金蝶和聚水潭映射列表
     *
     * @param bo 查询条件
     * @return 金蝶和聚水潭映射列表
     */
    @Override
    public List<JdAndJstMapVo> queryList(JdAndJstMapBo bo) {
        LambdaQueryWrapper<JdAndJstMap> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JdAndJstMap> buildQueryWrapper(JdAndJstMapBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JdAndJstMap> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPlatformCode()), JdAndJstMap::getPlatformCode, bo.getPlatformCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPlatform()), JdAndJstMap::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), JdAndJstMap::getMaterialCode, bo.getMaterialCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDataStatus()), JdAndJstMap::getDataStatus, bo.getDataStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getForbiddenStatus()), JdAndJstMap::getForbiddenStatus, bo.getForbiddenStatus());
        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JdAndJstMap::getId, idsList);
        }
        return lqw;
    }

    /**
     * 新增金蝶和聚水潭映射
     *
     * @param bo 金蝶和聚水潭映射
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JdAndJstMapBo bo) {
        JdAndJstMap add = MapstructUtils.convert(bo, JdAndJstMap.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改金蝶和聚水潭映射
     *
     * @param bo 金蝶和聚水潭映射
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JdAndJstMapBo bo) {
        JdAndJstMap update = MapstructUtils.convert(bo, JdAndJstMap.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JdAndJstMap entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除金蝶和聚水潭映射信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 查询金蝶和聚水潭映射列表所有数据
     *
     * @return 金蝶和聚水潭映射列表
     */
    @Override
    public List<JdAndJstMap> queryAll() {
        return baseMapper.selectList(null);
    }

    /**
     * 清空金蝶和聚水潭映射列表所有数据
     *
     * @return 金蝶和聚水潭映射列表
     */
    @Override
    public Integer deleteAll() {
        return baseMapper.delete(new QueryWrapper<>());
    }

    /**
     * 根据平台编码查询物料编码
     *
     * @param productNumber 商品编码
     * @return 金蝶和聚水潭映射
     */
    @Override
    public JdAndJstMap queryByPlatformCode(String productNumber) {
        LambdaQueryWrapper<JdAndJstMap> lqw1 = Wrappers.lambdaQuery();
        lqw1.eq(JdAndJstMap::getPlatformCode, productNumber);
        JdAndJstMap jdAndJstMap = baseMapper.selectOne(lqw1);
        return jdAndJstMap;

    }


}
